package com.qf.taobei.controller;

import com.qf.taobei.entity.RespBean;
import com.qf.taobei.entity.Store;
import com.qf.taobei.entity.User;
import com.qf.taobei.entity.bo.StoreUser;
import com.qf.taobei.entity.vo.Vo;
import com.qf.taobei.service.ProductService;
import com.qf.taobei.service.StoreService;
import com.qf.taobei.service.impl.ProductServiceImpl;
import com.qf.taobei.utils.OssUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @Author:CRT
 * @Date:
 * @Version:
 */
@Api(tags = "店铺模块")
@RestController
@RequestMapping("/Store")
public class StoreController {

    @Autowired
    StoreService storeService;
    //店铺、admin
    @PostMapping(value = "/registerStore",consumes = "multipart/*",
            headers = "content-type=multipart/form-data")
    /* @PostMapping(value ="/registerStore" )*/
    @ApiOperation("注册店铺功能")
    public RespBean registerStore(Store store, HttpServletRequest request,MultipartFile file) {
        RespBean respBean = storeService.addStore(store, request,file);
        return respBean;
    }

    @DeleteMapping("/deleteStores")
    @ApiOperation("真删除多个店铺功能")
    public RespBean deleteStoreTrue(int[] storeIds) {
        RespBean respBean = storeService.deleteStoresTrue(storeIds);
        return respBean;
    }

    @GetMapping("/queryStoreByUid")
    @ApiOperation("根据用户id查询用户店铺信息")
    public RespBean queryStoreByUid(int uid){
        RespBean respBean = storeService.queryStoreByUId(uid);
        return respBean;
    }

    //店铺
    @GetMapping("/queryStoreBySessionId")
    @ApiOperation("查询当前用户店铺信息")
    public RespBean queryStoreBySessionId(HttpServletRequest request){
        RespBean respBean = storeService.queryStoreBySessionId(request);
        return respBean;
    }

    @GetMapping("/saveStoreId")
    @ApiOperation("保存店铺id到session")
    public RespBean saveStoreId(int storeId,HttpServletRequest request){
        return storeService.saveStoreId(storeId,request);
    }

    @GetMapping("/selectStoreByStoreSessionId")
    @ApiOperation("根据session的storeId查询店铺信息")
    public RespBean selectStoreByStoreSessionId(HttpServletRequest request){
        int storeId = (Integer) request.getSession().getAttribute("storeId");
        RespBean respBean = storeService.queryStoreByStoreId(storeId);
        return respBean;
    }
    //店铺、admin
    @PostMapping(value = "/updateStore",consumes = "multipart/*",
            headers = "content-type=multipart/form-data")
    @ApiOperation("根据session的storeId修改店铺信息")
    public RespBean updateStore(HttpServletRequest request,Store store,MultipartFile file){
        return storeService.updateStoreByStoreId(request,store,file);
    }

    //店铺
    @PostMapping("/transferStore")
    @ApiOperation("转让店铺")
    public RespBean transferStore(User user,String yourName,String yourPassword,HttpServletRequest request){
        RespBean respBean = storeService.tranferStore(user, yourName, yourPassword, request);
        return respBean;
    }

    //店铺
    @PostMapping("/lougoutStore")
    @ApiOperation("注销店铺")
    public RespBean lougoutStore(String tel,String password,HttpServletRequest request){
        RespBean respBean = storeService.lougoutStore(tel, password, request);
        return respBean;
    }

    //admin
    @GetMapping("/deleteStoreByStoreIdFalse")
    @ApiOperation("根据单个店铺id伪删除店铺信息")
    public RespBean deleteStoreByStoreIdFalse(int storeId){
        RespBean respBean = storeService.deleteStoreByStoreIdFalse(storeId);
        return respBean;
    }
    //admin
    @GetMapping("/deleteStoreByStoreIdsFalse")
    @ApiOperation("根据多个店铺id伪删除店铺信息")
    public RespBean deleteStoreByStoreIdsFalse(int[] storeIds){
        RespBean respBean = storeService.deleteStoreByStoreIdsFalse(storeIds);
        return respBean;
    }

    //admin
    @GetMapping("/queryStoreUserAll")
    @ApiOperation("查询所有未删除店铺信息")
    public RespBean queryStoreUserAll(){
        StoreUser storeUser = new StoreUser();
        RespBean respBean = storeService.quertStoreUser(storeUser);
        return respBean;
    }

    //admin
    @GetMapping("/queryStoreUserByStoreInfo")
    @ApiOperation("根据店铺信息查询所有删除店铺信息 (店铺id 店铺状态 店铺等级，店家id 店家名字，店家电话查询)")
    public RespBean queryStoreUserByStoreInfo(StoreUser storeUser){

        RespBean respBean = storeService.quertStoreUser(storeUser);
        return respBean;
    }
    //admin
    @GetMapping("/queryStoreUserWolfer")
    @ApiOperation("查询所有已删除店铺信息")
    public RespBean queryStoreUserWolfer(){
        StoreUser storeUser = new StoreUser();
        RespBean respBean = storeService.queryStoreUserWolfer(storeUser);
        return respBean;
    }

    //admin
    @GetMapping("/queryStoreUserWolferByStoreInfo")
    @ApiOperation("根据店铺信息查询已删除店铺信息(店铺id 店铺状态 店铺等级，店家id 店家名字，店家电话查询)")
    public RespBean queryStoreUserWolferByStoreInfo(StoreUser storeUser){

        RespBean respBean = storeService.queryStoreUserWolfer(storeUser);
        return respBean;
    }
    @GetMapping("/getUser")
    @ApiOperation("获取用户信息")
    public RespBean getUser(HttpServletRequest request){

        User user = (User)request.getSession().getAttribute("user");
        if (user!=null){
            return RespBean.respSuccess("成功",user);
        }
        return RespBean.respError("获取用户信息失败");
    }


    @GetMapping("/getStoreTotal")
    @ApiOperation("获取店铺统计信息")
    public RespBean getStoreTotal(HttpServletRequest request){
        User user = (User)request.getSession().getAttribute("user");
        Integer uid = user.getId();
        RespBean respBean = storeService.getStoreTotalByUserId(uid);
        return respBean;
    }

    /**
     * 获取所有商品信息
     *
     * @return
     */
    @GetMapping("/queryAllProduct")
    @ApiOperation("获取商品列表")
    public Vo queryAllProduct() {
        Vo vo = storeService.queryAllProduct();
        return vo;
    }

    @GetMapping("/queryProductLimit")
    @ApiOperation("分页获取商品")
    public Vo queryAllProduct(HttpServletRequest request,int page,int limit,Integer flag) {
        Vo vo = storeService.queryProductLimit(request,page,limit,flag);
        return vo;
    }

    @GetMapping("/queryProductLimitByStoreId")
    @ApiOperation("根据店铺id分页获取商品")
    public Vo queryProductLimitByStoreId(HttpServletRequest request,int page,int limit,Integer flag) {
        Vo vo = storeService.queryProductLimitByStoreId(request,page,limit,flag);
        return vo;
    }

    @GetMapping("/queryProductByTypeId")
    @ApiOperation("根据类型查找商品")
    public Vo queryProductByTypeId(HttpServletRequest request,int id,int page,int limit,Integer flag){
        Vo vo = storeService.queryProductByTypeId(request,id, page, limit,flag);
        return vo;
    }

    @GetMapping("/queryProductByTypeIdAndStoreId")
    @ApiOperation("根据类型和店铺查找商品")
    public Vo queryProductByTypeIdAndStoreId(HttpServletRequest request,int id,int page,int limit,Integer flag){
        Vo vo = storeService.queryProductByTypeIdAndStoreId(request,id, page, limit,flag);
        return vo;
    }

    //测试阿里云对象存储功能
    @PostMapping(value = "/upload",consumes = "multipart/*",
            headers = "content-type=multipart/form-data")
    public RespBean upload(MultipartFile file,HttpServletRequest request) throws IOException {
        String upload = OssUtils.upload(file.getOriginalFilename(), file.getInputStream());
        request.getSession().setAttribute("storeImg",upload);
        return RespBean.respSuccess("三大",upload);
    }

    @GetMapping("/queryProductIdAndName")
    @ApiOperation("根据session里的id获取商品id和名字")
    public RespBean queryProductIdAndName(HttpServletRequest request){
        RespBean respBean = storeService.queryProductIdAndName(request);
        return respBean;
    }
}